//
//  WelfareDetailController.m
//  MindBox
//
//  Created by MS on 16-1-13.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "WelfareDetailController.h"
#import "WelfarePageView.h"
#import "Discover_WelfarePageTool.h"
#import "GoodsDetailController.h"
#import "GoodsController.h"
#import "UMSocial.h"
@interface WelfareDetailController ()<GiftPageViewDelegate>
@property (nonatomic,weak) WelfarePageView * welfareView;
@property (nonatomic,strong) NSArray * seach_KeyArray;
@end

@implementation WelfareDetailController

-(NSArray *)seach_KeyArray
{
    if (_seach_KeyArray==nil) {
        _seach_KeyArray = [[NSArray alloc] init];
    }
    return _seach_KeyArray;
}

-(WelfarePageView *)welfareView
{
    if (_welfareView==nil) {
        WelfarePageView * view = [[WelfarePageView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height-64)];
        [self.view addSubview:view];
        _welfareView = view;
        _welfareView.delegate = self;
    }
    return _welfareView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareGoods) name:@"Share" object:nil];
}


-(void)loadPageItemGoods_Data
{
    [Discover_WelfarePageTool discover_welfarePage_ItemGoods_DataWithCat_id:self.wel_cat_id seach_code:self.gos_id pages:++self.pages Success:^(NSArray *data) {
        [_welfareView.collectionView.mj_footer endRefreshing];
        [_welfareView.collectionView.mj_header endRefreshing];
        
        if (data.count) {
            [self.itemGoodsArray addObjectsFromArray:data];
        }
        
        self.welfareView.gift_ItemGoods = self.itemGoodsArray;
        
    } failure:^(NSError *error) {
        
    }];
    
}

-(void)loadHeaderPageData
{
    [Discover_WelfarePageTool discover_welfarePage_HeaderDataWithCat_id:self.wel_cat_id Success:^(Welfare_Detail *data) {
        
        self.seach_KeyArray = data.search_key_list;
        self.welfareView.welfareData = data;
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.welfareView.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadPageItemGoods_Data];
    }];
    
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.welfareView.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [weakSelf.itemGoodsArray removeAllObjects];
        [weakSelf loadPageItemGoods_Data];
    }];
}


-(void)giftSelectedViewDidClick:(GiftPageView *)giftView selectItem:(Gift_Cat_Id *)cat_id index:(NSInteger)index
{
    self.gos_id = [self.seach_KeyArray[index] search_key];
    [self.itemGoodsArray removeAllObjects];
    self.pages = 0;
    [self loadPageItemGoods_Data];
    
}

-(void)giftCollectionViewDidClick:(GiftPageView *)giftView itemGoods:(Gift_ItemGoods *)goods index:(NSInteger)index
{
    GoodsController * vc  = [[GoodsController alloc] init];
    vc.good_id = goods.goods_id;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)socailShare
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5683d356e0f55ae352001f4c"
                                      shareText:self.title
                                     shareImage:self.welfareView.headerView.imageView0.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToWechatTimeline,UMShareToTencent,UMShareToSms,UMShareToEmail,UMShareToQzone,UMShareToFacebook,UMShareToTwitter,nil]
                                       delegate:nil]; //
}

-(void)shareGoods
{
    [self socailShare];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    NSLog(@"WelfareDetailController卸载");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
